from tkinter import *
import time
class lw:
    def __init__(self, isplaying,root,time):
        self.isplaying = isplaying
        self.root = root
        self.time = time
    def update_time(self,current_time):
        if current_time != self.time:
            self.time = current_time    
    def show_lw(self):
        """下面为在画布上展示lw型指令的设计"""
        win = Toplevel()
        canvas = Canvas(win, width=1380, height=800, bg='White')
        canvas.pack(expand=True)
        # 最大帧数
        numIdx = 25            
        frames = [PhotoImage(file='gif/lw_final.gif',
                             format='gif -index %i' % (i)) for i in range(numIdx)]
        # 填充帧内容到frames
        def slow_speed():
            # 减速
            if(self.time <= 1500):
                self.time += 100

        def add_speed():
            # 加速
            if(self.time>100):
                self.time -=100

        def test_stop():
            """下面为播放与暂停的界面"""
            btn2.place(x=300, y=740)
            btn3.place(x=500, y=740)
            if self.isplaying == True:
                self.isplaying = False
                counter = '播放'
                btn4.config(text=str(counter))
            else:
                self.isplaying = True
                btn4.config(text=str("暂停"))
                update(self.stop_idx)

        def exit_win():
            self.time = 500
            self.isplaying = False
            win.destroy()

        def update(idx):    # 定时器函数
            frame = frames[idx]
            idx += 1         # 下一帧的序号：
            canvas.create_image(15, 15, anchor=NW, image=frame)
            # label.configure(image = frame) # 显示当前帧的图片
            # 0.1秒(500毫秒)之后继续执行定时器函数(update)
            if self.isplaying:
                self.root.after(self.time, update, idx % numIdx)
            else:
                self.stop_idx = idx

        btn1 = Button(win, text="结束", width=10, height=2, bg='White', fg='black',
                      font='宋体 12 bold', relief='raised', command=exit_win)
        btn2 = Button(win, text="加速", width=10, height=2, bg='White', fg='black',
                      font='宋体 12 bold', relief='raised', command=add_speed)
        btn3 = Button(win, text="减速", width=10, height=2, bg='White', fg='black',
                      font='宋体 12 bold', relief='raised', command=slow_speed)
        btn4 = Button(win, text="播放", width=10, height=2, bg='White', fg='black',
                      font='宋体 12 bold', relief='raised', command=test_stop)
        # btn5 = Button(win, text="绘图", width=10, height=2, bg='White', fg='black')

        btn1.place(x=100, y=740)
        btn4.place(x=700, y=740)
        win.after(0, update(0))
        # 结束后重新调用